﻿using Alison.Tools.HttpContact.Response;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Alison.EFCore.Dto;
using Alison.EFCore;

namespace Alison.Service
{
    public class MenuService : IMenuService
    {
        private readonly ILogger<MenuService> _logger;
        public MenuService(ILogger<MenuService> logger)
        {
            _logger = logger;
        }
        public async Task<ApiResponse> Add(MenuDto dto)
        {
            try
            {
                using var db = new AlisonDbContext();
                var model = await db.Menus.AddAsync(new EFCore.Entity.Menu()
                {
                    Menuauth = dto.MenuAuth,
                    Menucaption = dto.MenuCaption,
                    Menucode = dto.MenuCode,
                    Menuname = dto.MenuName,
                    Menunamespace = dto.MenuNameSpace
                });

                var ok = await db.SaveChangesAsync();
                return new ApiResponse(200, ok);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return new ApiResponse(500, ex.Message);
            }
        }

        public async Task<ApiResponse> Delete(int id)
        {
            try
            {
                using var db = new AlisonDbContext();
                var model = await db.Menus.FirstOrDefaultAsync(predicate: x => x.Id == id);
                if (model == null)
                {
                    return new ApiResponse(201, "The menu was not found");
                }
                db.Entry(model).State = EntityState.Deleted;
                if (await db.SaveChangesAsync() > 0)
                    return new ApiResponse(200, "");
                return new ApiResponse(201, $"Deleting post { id } failed when saving.");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return new ApiResponse(500, "");
            }
        }

        public async Task<ApiResponse> GetAll()
        {
            try
            {
                using var db = new AlisonDbContext();
                var model = await db.Menus.ToArrayAsync();

                return new ApiResponse(200, model.OrderBy(t => t.Id));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return new ApiResponse(500, ex.Message);
            }
        }

        public async Task<ApiResponse> GetAll(MenuDto dto)
        {
            try
            {
                using var db = new AlisonDbContext();
                var model = await db.Menus.Where(t => t.Menuname.Contains(dto.MenuName) || string.IsNullOrWhiteSpace(dto.MenuName)).ToArrayAsync();

                return new ApiResponse(200, model.OrderBy(t => t.Id));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return new ApiResponse(500, ex.Message);
            }
        }

        public async Task<ApiResponse> Save(MenuDto dto)
        {
            try
            {
                using var db = new AlisonDbContext();
                 
                var model = await db.Menus.FirstOrDefaultAsync(t=>t.Id == dto.Id);
                 
                model.Menuauth = dto.MenuAuth;
                model.Menucaption = dto.MenuCaption;
                model.Menucode = dto.MenuCode;
                model.Menuname = dto.MenuName;
                model.Menunamespace = dto.MenuNameSpace;

                db.Entry(model).State = EntityState.Modified;

                var ok = await db.SaveChangesAsync();
                return new ApiResponse(200, ok);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return new ApiResponse(500, ex.Message);
            }
        }
    }
}
